﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xyz.Service;
using Xyz.Service.DataTransferObjects;

namespace Xyz.Web.Admin.Flight
{
    public partial class CreateFlightSchedule : System.Web.UI.Page
    {
        FlightService fService = new FlightService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetAircrafts();
                SetAirports();
                ddlDepartAirport_SelectedIndexChanged(sender,e);
                ddlArrivalAirport_SelectedIndexChanged(sender, e);
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            FlightDTO fDTO = new FlightDTO();

            fDTO.DepartureAirport = ddlDepartAirport.SelectedValue;
            fDTO.ArrivalAirport = ddlArrivalAirport.SelectedValue;
            fDTO.DepartureDateTime = DateTime.Parse(txtDepartDate.Text + " " + tpDepartTime.GetTime);
            fDTO.ArrivalDateTime = DateTime.Parse(txtArrivalDate.Text + " " + tpArrivalDate.GetTime);
            fDTO.Distance = txtDistance.Text;

            if (!String.IsNullOrEmpty(txtTaxSurcharge.Text))
                fDTO.TaxSurcharge = Decimal.Parse(txtTaxSurcharge.Text);
            else
                fDTO.TaxSurcharge = 0;

            fDTO.FlightCode = txtFlightNumber.Text;
            fDTO.FlightStatus = txtStatus.Text;
            fDTO.ArrivalTerminal = ddlArrivalTerminal.SelectedValue;
            fDTO.DepartureTerminal = ddlDepartTerminal.SelectedValue;
            fDTO.AircraftId = Convert.ToInt32(ddlAircraft.SelectedValue);
            fService.InsertFlight(fDTO);

            Response.Redirect("SearchFlightSchedule.aspx");
        }

        private void SetAircrafts()
        {
            Dictionary<string, string> aircrafts = new Dictionary<string, string>();
            List<AircraftDTO> aircraftList = fService.GetAircrafts();
            foreach (AircraftDTO aircraft in aircraftList)
            {
                aircrafts.Add(aircraft.AircraftId.ToString(), aircraft.AircraftName);
            }

            ddlAircraft.DataTextField = "Value";
            ddlAircraft.DataValueField = "Key";
            ddlAircraft.DataSource = aircrafts;
            ddlAircraft.DataBind();
        }

        private void SetAirports()
        {
            Dictionary<string, string> airports = new Dictionary<string, string>();
            List<AirportDTO> airportList = fService.GetAirports();
            foreach (AirportDTO airport in airportList)
            {
                airports.Add(airport.AirportCode, airport.AirportName);
            }

            ddlDepartAirport.DataTextField = "Value";
            ddlDepartAirport.DataValueField = "Key";
            ddlDepartAirport.DataSource = airports;
            ddlDepartAirport.DataBind();

            ddlArrivalAirport.DataTextField = "Value";
            ddlArrivalAirport.DataValueField = "Key";
            ddlArrivalAirport.DataSource = airports;
            ddlArrivalAirport.DataBind();
        }

        private Dictionary<string, string> GetTerminalByAirport(string airportCode)
        {
            Dictionary<string, string> terminals = new Dictionary<string, string>();
            List<TerminalDTO> terminalList = fService.GetTerminalByAirportId(airportCode);
            foreach (TerminalDTO terminal in terminalList)
            {
                terminals.Add(terminal.TerminalCode, terminal.TerminalDescription);
            }

            return terminals;
        }

        protected void ddlDepartAirport_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDepartTerminal.DataTextField = "Value";
            ddlDepartTerminal.DataValueField = "Key";
            ddlDepartTerminal.DataSource = GetTerminalByAirport(ddlDepartAirport.SelectedValue);
            ddlDepartTerminal.DataBind();
        }

        protected void ddlArrivalAirport_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlArrivalTerminal.DataTextField = "Value";
            ddlArrivalTerminal.DataValueField = "Key";
            ddlArrivalTerminal.DataSource = GetTerminalByAirport(ddlArrivalAirport.SelectedValue);
            ddlArrivalTerminal.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("SearchFlightSchedule.aspx");
        }
    }
}